package com.task;

import com.model.News;
import com.util.ActiveManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableScheduling
public class SpringTask {
	@Autowired
    ActiveManager activeManager;
	//@Scheduled(cron="0 0 6,8,10,14,18,20 * * ?")
    @Scheduled(cron="10 * * * * ?")
	public void m1() throws Exception {
		//py爬取的数据success
        List<String> list1 = new ArrayList<>();
        list1.add("D:\\data\\guoji.py");
        list1.add("D:\\data\\redian.py");
        list1.add("D:\\data\\sehui.py");
        for(String str :list1){
            execCmd("python  "+str);
        }
        List<String> list = new ArrayList<>();
        list.add("D:\\data\\py.txt");
        list.add("D:\\data\\society.txt");
        list.add("D:\\data\\mil.txt");
        for(String str :list){
            readFile02(str);
        }
        /*news
        System.out.println(str);*/
        //Destination destination = new ActiveMQQueue("beyondLiQueueTest");
        //传入队列以及值
        //activeManager.send(destination, news.toString());
        //activeManager.delaySend(news.toString(), "top", 5000L);
	}
    public  void readFile02(String path) throws Exception {
        // 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
        List<News> list = new ArrayList<News>();
        FileInputStream fis = new FileInputStream(path);
        // 防止路径乱码   如果utf-8 乱码  改GBK     eclipse里创建的txt  用UTF-8，在电脑上自己创建的txt  用GBK
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null) {
            System.out.println("表准");
            activeManager.delaySend(line, "top", 5000L);
        }
        br.close();
        isr.close();
        fis.close();

    }
    private static String execCmd(String cmd) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        // 执行命令
        Process process = runtime.exec(cmd);
        // 接收命令执行的输出信息
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        String info = "";
        StringBuilder sb = new StringBuilder();
        while ((info = infoReader.readLine()) != null) {
            sb.append(info);
        }
        // 接收命令执行的错误信息
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(
                process.getErrorStream()));
        String error = "";
        while ((error = errorReader.readLine()) != null) {
            sb.append(error);
        }
        return sb.toString();
    }
}
